package com.xrd.serialhelper;


import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.xrd.uart.interaction.ModuleConnector;
import com.xrd.uart.interaction.RXForResultListener;
import com.xrd.uart.interaction.RXTXListener;
import com.xrd.uart.uart.ReaderConnector;
import com.xrd.uart.uart.TtyS2ReaderHelper;
import com.xrd.uart.uart.TtyS3ReaderHelper;
import com.xrd.uart.uart.rxobserver.RXObserver;
import com.xrd.uart.util.DataUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Observer;
import java.util.Vector;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.

    private InputStream mInputStream;
    private TextView tv;
    private OutputStream outputStream;

    private TtyS2ReaderHelper mReaderHelper;
    private Observer mInventoryObserver;
    private ModuleConnector connector;
    private TtyS3ReaderHelper ttyS3ReaderHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.sample_text);
        Button buttom = findViewById(R.id.buttom);

        Vector<byte[]> objects = new Vector<>();


        connector = new ReaderConnector();
        mReaderHelper = TtyS2ReaderHelper.getDefaultHelper();
        connector.connectCom(mReaderHelper, 9600);////////......1111111111
//        mReaderHelper = TtyS2ReaderHelper.getDefaultHelper();
//        connector.setTimeOut(50);

        mReaderHelper.setLogIsShow(true);

        mInventoryObserver = new RXObserver() {
            @Override
            public void analyData(byte[] arg) {
                byte[] arg1 = (byte[]) arg;
                Log.d(TAG, "analyData:RXObserver " + DataUtils.ByteArrToHex(arg1));
                Log.d(TAG, "analyData:RXObserver " + new String(arg1));
            }


        };

        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();

//        mReaderHelper.sendCommandHasMultiReSult(ByteUtil.hexStr2bytes(cmd));











        /*
         *
         * E4 B8 AD E5 8D 8E   UTF-8
         *
         */

//        mReaderHelper.setRXTXListener(new RXTXListener() {
//            @Override
//            public void receiveData(byte[] var1) {
//
//                String arrToHex = DataUtils.ByteArrToHex(var1);
////                Log.d(TAG, "receiveData: " +  arrToHex);
//                final String s = new String(var1);
//                Log.d(TAG, "receiveData: " + s);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
//                    }
//                });
//
////                Log.d(TAG, "receiveData: " + new String(var1, StandardCharsets.UTF_8));
////                Log.d(TAG, "receiveData: " + new String(var1, StandardCharsets.ISO_8859_1));
////                Log.d(TAG, "receiveData: " + new String(var1, StandardCharsets.US_ASCII));
////                Log.d(TAG, "receiveData: " + new String(var1, StandardCharsets.UTF_16));
////                Log.d(TAG, "receiveData: " + arrToHex.toString());
//
//            }
//
//            @Override
//            public void sendData(byte[] var1) {
//                Log.d(TAG, "sendData: " + DataUtils.ByteArrToHex(var1));
//            }
//
//            @Override
//            public void onLostConnect() {
//
//            }
//        });


//        mReaderHelper.registerObserver(mInventoryObserver);
//        buttom.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                sendResult("中华人民");
////                mReaderHelper.sendCommand(DataUtils.HexToByteArr("123456"));
////                mReaderHelper.sendCommand("qgs".getBytes());
//                mReaderHelper.setEndFlag((byte) 0x0A);
//                byte[] bytes = mReaderHelper.sendCommandHasMultiReSult("qgsa".getBytes());
//                if (bytes != null) {
//                    Log.d(TAG, "onClick: " + new String(bytes));
//                }
//            }
//        });

    }

    private void sendResult(String s) {
//        return mReaderHelper.sendCommandForReSult(DataUtils.HexToByteArr(s), new RXForResultListener() {
        mReaderHelper.sendCommandForReSultListener(s.getBytes(), new RXForResultListener() {

            @Override
            public void receiveData(byte[] var1) {
                Log.d(TAG, "receiveData: Result" + DataUtils.ByteArrToHex(var1));
            }

            @Override
            public void sendData(byte[] var1) {//
                Log.d(TAG, "sendData: Result" + DataUtils.ByteArrToHex(var1));
                Log.d(TAG, "sendData: Result" + new String(var1));
            }


            @Override
            public void onTimeOut() {
                Log.d(TAG, "sendData:  onTimeOut");
            }

            @Override
            public void onLostConnect() {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        mReaderHelper.unRegisterObserver(mInventoryObserver);
        connector.disConnect();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void total(View view) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                //01030000005045f6
              String cmd = "0103000000E6c440";
//                String cmd = "01030000005045f6";
                byte e6 = DataUtils.HexToByte("e6");
                int e61 = DataUtils.HexToInt("e6");
                byte[] bytes = mReaderHelper.sendCommandTotalReSult(DataUtils.HexToByteArr(cmd));
//                byte[] bytes = mReaderHelper.sendCommandHasMultiReSult(DataUtils.HexToByteArr(cmd));
                if (bytes != null) {
                    String msg = "total: " + bytes.length + "\ncontent:" + DataUtils.ByteArrToHex(bytes);
                    Log.d(TAG, msg);
//                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }
        }).start();



//

    }

}
